/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jude.rollviewpagerdome.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.Context;

import com.jude.rollviewpager.Until;
import com.jude.rollviewpagerdome.LoopAbility;
import com.jude.rollviewpagerdome.NetImageAbility;
import com.jude.rollviewpagerdome.ResourceTable;
import com.jude.rollviewpagerdome.SimpleAbility;

/**
 * PageSliderAdapter 适配器
 *
 * @since 2021-03-23
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button button1;
    private Button button2;
    private Button button3;
    private Context context;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        context = this;
        initview();
    }

    private void initview() {
        button1 = (Button) findComponentById(ResourceTable.Id_Button1);
        button1.setClickedListener(this::onClick);
        button2 = (Button) findComponentById(ResourceTable.Id_Button2);
        button2.setClickedListener(this::onClick);
        button3 = (Button) findComponentById(ResourceTable.Id_Button3);
        button3.setClickedListener(this::onClick);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_Button1:
                Intent secondIntent = new Intent(); // 指定待启动FA的bundleName和abilityName
                Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(context.getBundleName())
                    .withAbilityName(SimpleAbility.class.getName())
                    .build();
                secondIntent.setOperation(operation); // 通过AbilitySlice的startAbility接口实现启动另一个页面
                startAbility(secondIntent);

                break;
            case ResourceTable.Id_Button2:
                Intent secondIntent2 = new Intent(); // 指定待启动FA的bundleName和abilityName
                Operation operation2 = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(context.getBundleName())
                    .withAbilityName(LoopAbility.class.getName())
                    .build();
                secondIntent2.setOperation(operation2); // 通过AbilitySlice的startAbility接口实现启动另一个页面
                startAbility(secondIntent2);
                break;
            case ResourceTable.Id_Button3:
                Until.setUge(true);
                Intent secondIntent1 = new Intent(); // 指定待启动FA的bundleName和abilityName
                Operation operation1 = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(context.getBundleName())
                    .withAbilityName(NetImageAbility.class.getName())
                    .build();
                secondIntent1.setOperation(operation1); // 通过AbilitySlice的startAbility接口实现启动另一个页面
                startAbility(secondIntent1);
                break;
            default:
                break;
        }
    }
}
